/*
* @lc app=leetcode.cn id=1636 lang=javascript
*
* [1636] 按照频率将数组升序排序
*/

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var frequencySort = function (nums) {
  const map = {};
  nums.forEach(num => {
    map[num] = map[num] ? map[num] + 1 : 1;
  })
  const entries = Object.entries(map).sort((a, b) => {
    if (a[1] !== b[1]) {
      return a[1] - b[1];
    } else {
      return b[0] - a[0];
    }
  });
  const res = [];
  for (let i = 0; i < entries.length; i++) {
    res.push(...Array(entries[i][1]).fill(entries[i][0]))
  }
  return res;
};
// @lc code=end

